class Solution {
public:
    int sumIndicesWithKSetBits(vector<int>& nums, int k) {
        int ans = 0;
        int idx = 0;
        for (auto x : nums) {
            int cnt = 0;
            for (int i = 0; i < 31; i++) {
                if ((1 << i) & idx)cnt++;
            }
            if (cnt == k)ans += x;
            idx++;
        }
        return ans;
    }
};